package cn.belong.jk.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.belong.jk.dao.ContractDao;
import cn.belong.jk.entity.Contract;
import cn.belong.jk.entity.ContractVO;

@Service("contractService")
public class ContractServiceImpl implements ContractService {

	@Autowired
	ContractDao contractdao;
	
	public List<Contract> find(Map paraMap) {
		// TODO Auto-generated method stub
		return contractdao.find(paraMap);
	}

	public Contract get(Serializable id) {
		// TODO Auto-generated method stub
		return contractdao.get(id);
	}

	public void insert(Contract entity) {
		entity.setId(UUID.randomUUID().toString()); 
		entity.setState(0);//默认为草稿  
		contractdao.insert(entity);

	}

	public void update(Contract entity) {
		contractdao.update(entity);

	}

	public void delete(Serializable[] ids) {  
	      
	    try {  
	        //删除合同时删除合同货物下的所有附件  
	    	contractdao.deleteExtByContractById(ids);  
	    } catch (Exception e1) {  
	        // TODO Auto-generated catch block  
	        e1.printStackTrace();  
	    }  
	      
	    try {  
	        //删除合同时同时删除合同下的货物  
	    	contractdao.deleteByContractById(ids);  
	    } catch (Exception e) {  
	        // TODO Auto-generated catch block  
	        e.printStackTrace();  
	    }  
	    contractdao.delete(ids);  
	}  
	  
	  
	 
	public void deleteById(Serializable id) {  
	    Serializable[] ids={id};  
	      
	    try {  
	        //删除合同时删除合同货物下的所有附件  
	    	contractdao.deleteExtByContractById(ids);  
	    } catch (Exception e1) {  
	        // TODO Auto-generated catch block  
	        e1.printStackTrace();  
	    }  
	      
	    try {  
	        //删除合同时同时删除合同下的货物  
	    	contractdao.deleteByContractById(ids);  
	    } catch (Exception e) {  
	        // TODO Auto-generated catch block  
	        e.printStackTrace();  
	    }  
	    contractdao.deleteById(id);  
	}  

	public void submit(Serializable[] ids) {
        //默认刚建完的是草稿，建完之后确定没问题就上报  
        //0草稿 1已上报  
         Map map=new HashMap();  
         map.put("state",1);//1已上报  
         map.put("ids", ids);  
           
         contractdao.updateState(map);  
		
	}

	public void cancel(Serializable[] ids) {
        Map map=new HashMap();  
        map.put("state",0);//0  
        map.put("ids", ids);  
          
        contractdao.updateState(map);  
		
	}

	public ContractVO view(String contractId) {
		return contractdao.view(contractId);  
	}


}
